package tang.server.processor;

import tang.server.Request;
import tang.server.ZkServer;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author Tang
 * @classname PreRequestProcessor
 * @description [  ]
 * @date 2021/2/28 22:42
 */
public class PreRequestProcessor extends Thread implements RequestProcessor{

    private RequestProcessor nextProcessor;
    private ZkServer zkServer;
    private LinkedBlockingQueue<Request> blockingQueue = new LinkedBlockingQueue<>();

    public PreRequestProcessor(ZkServer zkServer,RequestProcessor nextProcessor) {
        this.nextProcessor = nextProcessor;
        this.zkServer = zkServer;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Request take = blockingQueue.take();

                this.nextProcessor.processRequest(take);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void processRequest(Request request) {
        nextProcessor.processRequest(request);
    }

    @Override
    public void shutdown() {

    }

    public void addProcessorRequest(Request request) {
        blockingQueue.offer(request);
    }
}